package cn.wang.web.cargo;

import cn.wang.entity.cargo.Contract;
import cn.wang.entity.cargo.ContractProduct;
import cn.wang.service.cargo.ContractProductService;
import cn.wang.service.cargo.ContractService;
import cn.wang.web.BaseAction;
import javafx.scene.control.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by john on 2017/11/9.
 * <p>
 * 1.创建工作簿workbook
 * 2.创建一个工作表sheet
 * 3.创建一个行对象row(下标起始值为0)
 * 4.创建一个单元格对象cell(下标起始值为0)
 * 5.给单元格设置内容
 * 6.设置单元格的样式，设置字体和字体大小
 * 7.保存，关闭流对象
 */
public class OutProductAction extends BaseAction {
    //根据船期打印出或表
    private String inputDate;
    @Autowired
    private ContractProductService contractProductService;
    private List<ContractProduct> productList;

    public String toedit() {

        return "toedit";
    }
  /*  public String print() throws IOException {
        //初始化行数
        int rowNO=0;
        int cellNo = 1;
        //工作簿
        Workbook wb=new HSSFWorkbook();
        //工作表
        Sheet sheet= wb.createSheet("出货表");
         //行对象
        Row row=sheet.createRow(rowNO++);//大标题
        //合并
        sheet.addMergedRegion(new CellRangeAddress(0,0,1,9));
        //列对象
        Cell cell=row.createCell(cellNo);
        String title=inputDate.replace("-","年")+"月份出货表";
        cell.setCellValue(title);
        //设置内容
        cell.setCellStyle(bigTitle(wb));
        //设置小标题

         String[] tilets = new String[]{"客户","订单号","货号","数量","工厂","工厂交期","船期","贸易条款"};
         row=sheet.createRow(rowNO++);

        for (String tilet:tilets) {
            cell=row.createCell(cellNo++);
            //设置样式
            cell.setCellStyle(title(wb));
            //设置第二行数据
            cell.setCellValue(tilet);
        }
       //-------显示出货列表------

        SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM");
        productList=contractProductService.findByshipTime(inputDate);
        //放到数组里方便取数据
        for (ContractProduct cp:productList) {
            Contract c=cp.getContract();
            String[] values=new String[]{c.getCustomName(),c.getContractNo(),cp.getProductNo(),cp.getCnumber().toString(),cp.getFactory().getFactoryName(),fmt.format(c.getDeliveryPeriod()),fmt.format(c.getShipTime()),c.getTradeTerms()};
          //将列数重置为1
            cellNo=1;
            row=sheet.createRow(rowNO++);
            for(String value:values){
                cell=row.createCell(cellNo++);
                if(value!=null){
                    cell.setCellValue(value);

                }
                cell.setCellStyle(text(wb));
            }
        }
        //-------------------文件下载---
       //response输出流打印数据
        HttpServletResponse response= ServletActionContext.getResponse();
        //告诉浏览器这是文件下载
        response.setContentType("appliction/x-download");
        //处理下载文件名中文问题
        String fileName=new String((title+"xls").getBytes("gbk"),"iso8859-1");
        //下载头设置
        response.addHeader("Content-disposition","attachment;filename="+fileName);
        OutputStream out= response.getOutputStream();
        wb.write(out);
        out.flush();
        return null;
    }*/

    /**
     * 根据船期打印出货表(利用模板方式)
     *
     * @return
     */
    //大标题的样式
/*    public String print() throws IOException {
        //初始化行数
        int rowNO=0;
        int cellNo = 1;
        //获取模板相对路径
        String filepath=ServletActionContext.getServletContext().getRealPath("/make/xlsprint/tOUTPRODUCT.xls");
       InputStream in=new FileInputStream(filepath);
        Workbook wb=new HSSFWorkbook(in);
        Sheet sheet=wb.getSheetAt(0);

        Row row=sheet.getRow(rowNO++);//大标题
        Cell cell=row.getCell(1);
        String title=inputDate.replace("-","年")+"月份出货表";
        cell.setCellValue(title);

        //设置小标题
        rowNO++;
        //数据
        row=sheet.getRow(2);
        //获取样板格式
        CellStyle cellStyle1=row.getCell(1).getCellStyle();
        CellStyle cellStyle2=row.getCell(2).getCellStyle();
        CellStyle cellStyle3=row.getCell(3).getCellStyle();
        CellStyle cellStyle4=row.getCell(4).getCellStyle();
        CellStyle cellStyle5=row.getCell(5).getCellStyle();
        CellStyle cellStyle6=row.getCell(6).getCellStyle();
        CellStyle cellStyle7=row.getCell(7).getCellStyle();
        CellStyle cellStyle8=row.getCell(8).getCellStyle();
        //将格式存入数组方便取出
        CellStyle[] cellStyle=new CellStyle[]{cellStyle1,cellStyle2,cellStyle3,cellStyle4,cellStyle5,cellStyle6,cellStyle7,cellStyle8};
        //-------显示出货列表------
        SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM");
        productList=contractProductService.findByshipTime(inputDate);
        //放到数组里方便取数据
        for (ContractProduct cp:productList) {
            Contract c=cp.getContract();
            String[] values=new String[]{c.getCustomName(),c.getContractNo(),cp.getProductNo(),cp.getCnumber().toString(),cp.getFactory().getFactoryName(),fmt.format(c.getDeliveryPeriod()),fmt.format(c.getShipTime()),c.getTradeTerms()};
            //将列数重置为1
            cellNo=1;
            row=sheet.createRow(rowNO++);
            for(String value:values){
                cell=row.createCell(cellNo);
                cell.setCellStyle(cellStyle[cellNo++-1]);
                if(value!=null){
                    cell.setCellValue(value);
                }
            }
        }
        //-------------------文件下载---
        //response输出流打印数据
        HttpServletResponse response= ServletActionContext.getResponse();
        //告诉浏览器这是文件下载
        response.setContentType("appliction/x-download");
        //处理下载文件名中文问题
        String fileName=new String((title+"xls").getBytes("gbk"),"iso8859-1");
        //下载头设置
        response.addHeader("Content-disposition","attachment;filename="+fileName);
        OutputStream out= response.getOutputStream();
        wb.write(out);
        out.flush();
        return null;
    }*/

    //根据模板打印出货表
    public String print() throws IOException {
        //读取取模板对象 根据路径获取
        String filepath = ServletActionContext.getServletContext().getRealPath("make/xlsprint/txOutProduct.xls");
        //输入流获取文本对象
        InputStream is = new FileInputStream(filepath);
        //创建工作簿
        Workbook wb = new HSSFWorkbook(is);
        //初始化行 列
        int rowNo = 0;
        int cellNo = 1;
        //获取工作表
        Sheet sheet = wb.getSheetAt(0);
        //获取大标题
        Row row = sheet.getRow(rowNo++);
        String title = inputDate.replace("-", "年") + "月份出货表";
        Cell cell = row.getCell(cellNo++);
        cell.setCellValue(title);
        //設置小标题
        rowNo++;
        row = sheet.getRow(2);

        //获取数据列表
        productList = contractProductService.findByshipTime(inputDate);
        //获取样板格式
        CellStyle cellStyle1 = row.getCell(1).getCellStyle();
        CellStyle cellStyle2 = row.getCell(2).getCellStyle();
        CellStyle cellStyle3 = row.getCell(3).getCellStyle();
        CellStyle cellStyle4 = row.getCell(4).getCellStyle();
        CellStyle cellStyle5 = row.getCell(5).getCellStyle();
        CellStyle cellStyle6 = row.getCell(6).getCellStyle();
        CellStyle cellStyle7 = row.getCell(7).getCellStyle();
        CellStyle cellStyle8 = row.getCell(8).getCellStyle();
        //存入数组方便取
        CellStyle[] cellStyles = new CellStyle[]{cellStyle1, cellStyle2, cellStyle3, cellStyle4, cellStyle5, cellStyle6, cellStyle7, cellStyle8};

        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM");
        for (ContractProduct cp : productList) {
            Contract c = cp.getContract();
            String[] values = new String[]{c.getCustomName(), c.getContractNo(), cp.getProductNo(), cp.getCnumber().toString(),
                    cp.getFactory().getFactoryName(), fmt.format(c.getDeliveryPeriod()),
                    fmt.format(c.getShipTime()), c.getTradeTerms()};
            //设置cellNo为1
            cellNo = 1;
            row = sheet.createRow(rowNo++);
            for (String value : values) {
                cell = row.createCell(cellNo);
                //设置样式
                cell.setCellStyle(cellStyles[cellNo++ - 1]);
                if (value != null) {
                    cell.setCellValue(value);
                }
            }
        }
        //文件下载根据response
        //获取response

        HttpServletResponse response = ServletActionContext.getResponse();
        //告诉浏览器是文件下载
        response.setContentType("appliction/x-download");
        //设置下载文件名
        String loadName = new String((title + "xls").getBytes("gbk"), "iso8859-1");
        //下载设置值
        response.addHeader("Content-disposition", "attachment;filename=" + loadName);
        OutputStream out = response.getOutputStream();
        //poi打印
        wb.write(out);
        return null;
    }

    public CellStyle bigTitle(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 16);
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);                    //字体加粗

        style.setFont(font);

        style.setAlignment(CellStyle.ALIGN_CENTER);                    //横向居中
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);        //纵向居中

        return style;
    }

    //小标题的样式
    public CellStyle title(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short) 12);

        style.setFont(font);

        style.setAlignment(CellStyle.ALIGN_CENTER);                    //横向居中
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);        //纵向居中

        style.setBorderTop(CellStyle.BORDER_THIN);                    //上细线
        style.setBorderBottom(CellStyle.BORDER_THIN);                //下细线
        style.setBorderLeft(CellStyle.BORDER_THIN);                    //左细线
        style.setBorderRight(CellStyle.BORDER_THIN);                //右细线

        return style;
    }

    //文字样式
    public CellStyle text(Workbook wb) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short) 10);

        style.setFont(font);

        style.setAlignment(CellStyle.ALIGN_LEFT);                    //横向居左
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);        //纵向居中

        style.setBorderTop(CellStyle.BORDER_THIN);                    //上细线
        style.setBorderBottom(CellStyle.BORDER_THIN);                //下细线
        style.setBorderLeft(CellStyle.BORDER_THIN);                    //左细线
        style.setBorderRight(CellStyle.BORDER_THIN);                //右细线

        return style;
    }

    public void setInputDate(String inputDate) {
        this.inputDate = inputDate;
    }
}
